package com.cdz.charger.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdz.charger.dto.ReservationDto;
import com.cdz.charger.util.EmailUtil;
import com.cdz.charger.vo.ReservationVo;
import com.cdz.common.core.domain.entity.SysUser;
import com.cdz.common.utils.DateUtils;
import com.cdz.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cdz.charger.mapper.ReservationMapper;
import com.cdz.charger.domain.Reservation;
import com.cdz.charger.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;

/**
 * 预约Service业务层处理
 *
 * @author baefengk2
 * @date 2025-03-10
 */
@Service
@Slf4j
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询预约
     *
     * @param id 预约主键
     * @return 预约
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return getById(id);
    }

    /**
     * 查询预约列表
     *
     * @param dto 预约
     * @return 预约
     */
    @Override
    public List<ReservationVo> selectReservationList(ReservationDto dto) {
        return reservationMapper.selectReservationList(dto);
    }

    /**
     * 新增预约
     *
     * @param reservation 预约
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        reservation.setStatus(1);

        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约
     *
     * @param reservation 预约
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约
     *
     * @param ids 需要删除的预约主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息
     *
     * @param id 预约主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return removeById(id) ? 1 : 0;
    }


    /**
     * 定时通知用户预约时间
     *
     * @return
     */
    public void sendReservationMsg(){
        //查询预约开始时间等于当前时间的预约信息 并且状态正常
        log.info("开始发送预约通知，当前时间：{}", LocalDateTime.now());
        LambdaQueryWrapper<Reservation> lqw = new LambdaQueryWrapper<Reservation>()
                .le(Reservation::getStartTime, LocalDateTime.now())
                .eq(Reservation::getStatus,1);

        List<Reservation> reservations = reservationMapper.selectList(lqw);

        if (CollectionUtils.isEmpty(reservations)){
            log.info("没有预约信息需要通知");
            return;
        }

        //发送短信通知用户
        reservations.forEach(reservation -> {
            Long userId = reservation.getUserId();
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            String email = sysUser.getEmail();
            String userName = sysUser.getUserName();
            String msg = "尊敬的用户：" + userName + "，您好！您的预约已经开始，请及时使用充电桩。";
            EmailUtil.sendEmail(email, "预约通知", msg);

            log.info("预约通知发送成功，用户名：{}，邮箱：{}，预约开始时间：{}",userName,email,reservation.getStartTime());
            //设置状态为失效
            reservation.setStatus(0);
            reservationMapper.updateById(reservation);
        });

    }


}
